﻿
var dateFormat = 'd MM yy';
var rollReservationEasing = 'easeOutQuint';
var initialPosition;
var maxPeople = 12;
function initSelectPeople() {
    initialPosition = $('.rr-peoples-sel').position();
    $('.rr-peoples-sel').css({
        position: 'absolute',
        top: initialPosition.top + 'px',
        left: initialPosition.left + 'px',
        cursor: 'pointer'
    });
}

function openSelectPeople(selectValue) {
    $('.roll-reservation-parent').css('overflow', 'visible');
    $('.rr-peoples-sel').html('');
    $('.rr-peoples-sel').css('height', 'auto').css('z-index', '1000');
    $('.rr-peoples-sel').css('top', (initialPosition.top - 23 * (selectValue - 1)) + 'px');
    $('.rr-peoples-sel').css('border', 'solid 1px #333');
    for (var i = 0; i < maxPeople; i++) {
        $('.rr-peoples-sel').append('<span style="display:block;text-align:center;height:23px;width:100%">' + (i + 1) + '</span>');
    }
    $('.rr-peoples-sel span').click(function (e) {
        closeSelectPeople($(this).text());
        e.stopPropagation();
        e.preventDefault();
    });
}

function closeSelectPeople(selectedValue) {
    $('.roll-reservation-parent').css('overflow', 'hidden');
    $('.rr-peoples-sel').html('');
    $('.rr-peoples-sel').css('height', '').css('z-index', '1');
    $('.rr-peoples-sel').css('border', 'none');
    $('.rr-peoples-sel').css('top', initialPosition.top + 'px');
    $('.rr-peoples-sel').text(selectedValue);
    $('input[name=rr-peoples]').val(selectedValue);
}

function openReservations() {
    $('.roll-reservation-parent').height(300);
    $('.roll-reservation').animate({
        top: '0px'
    }, 700, rollReservationEasing);
    $('.rr-action-name').text(language.close.toUpperCase());
    $('.rr-action-ud').removeClass('rr-open-down').addClass('rr-open-up');
}

function closeReservations() {
    $('.roll-reservation').animate({
        top: '-205px'
    }, 500, rollReservationEasing, function () { $('.roll-reservation-parent').height(95); });
    $('.rr-action-name').text(language.chooseDate.toUpperCase());
    $('.rr-action-ud').removeClass('rr-open-up').addClass('rr-open-down');
}

$(function () {

    var crnDate = new Date();

    var monthNames = $.datepicker._defaults['monthNames2'];
    if (monthNames)
        $.datepicker._defaults['monthNames'] = monthNames;

    $('.rr-date-1').datepicker({
        dateFormat: dateFormat,
        defaultDate: crnDate
    });
    $('.rr-date-1').datepicker('setDate', crnDate);
    $('.rr-date-1').val($.datepicker.formatDate(dateFormat, $('.rr-date-1').datepicker('getDate')));

    $('.rr-date-2').datepicker({
        dateFormat: dateFormat,
        defaultDate: new Date(crnDate.getFullYear(), crnDate.getMonth(), crnDate.getDate() + 1)
    });
    $('.rr-date-2').datepicker('setDate', new Date(crnDate.getFullYear(), crnDate.getMonth(), crnDate.getDate() + 1));
    $('.rr-date-2').val($.datepicker.formatDate(dateFormat, $('.rr-date-2').datepicker('getDate')));

    $('.rr-date-1').change(function () {
        var d = $(this).datepicker('getDate');
        $('.rr-date-2').datepicker('setDate', new Date(d.getFullYear(), d.getMonth(), d.getDate() + 1));

    });

    $('a[href=calculate]').click(function (e) {
        e.preventDefault();
        $('.rr-date').each(function () {
            var date = $.datepicker.formatDate('dd.mm.yy', $(this).datepicker('getDate'));
            $(this).parent().find('input[type=hidden]').val(date);
        });
        $('input[name=submitReservation]').val('1');
        $('form').submit();
    });


    $('.roll-reservation-parent').css('left', Math.ceil(($(window).width() - 960) / 2) + 'px');

    $('.roll-reservation .rr-action').click(function (e) {
        e.stopPropagation();
        e.preventDefault();
        if ($(this).data('open') == '1') {
            closeReservations();
            $(this).data('open', '0');
        } else {
            openReservations();
            $(this).data('open', '1');
        }
    });

    initSelectPeople();
    $('.rr-peoples-sel').click(function () {
        openSelectPeople($('input[name=rr-peoples]').val());
    });

});
